package com.shwread.android.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.netease.nim.demo.base.YXBaseActivity;
import com.shwread.android.qysw.QyReaderApp;
import com.shwread.android.qysw.activity.BaseSimpleActivity;
import com.shwread.android.qysw.activity.NewLoginActivity;
import com.shwread.android.qysw10000243.R;

import org.json.JSONException;
import org.json.JSONObject;

import logic.constants.DefaultConsts;
import logic.util.Util;


public class SingleSignOnDialog
        extends
        Dialog
        implements
        android.view.View.OnClickListener
//        extends
//        CommonDialog
{
	protected TextView tvText;
	protected TextView tvOk;
	protected View     vCancel;
	
	protected TextView vTitle;
	private Context    context;
	private String     json;
	
	public SingleSignOnDialog(Context context) {
		super(context, R.style.common_dialog);
		this.context = context;
		
	}
	
	public void setJson(String json) {
		this.json = json;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		
		View view = LayoutInflater.from(context).inflate(R.layout.offline_dialog, null);
		setContentView(view);
		tvText = (TextView) view.findViewById(R.id.common_dialog_text);
		tvOk = (TextView) view.findViewById(R.id.common_dialog_ok);
		vCancel = view.findViewById(R.id.common_dialog_cancel);
		vTitle = (TextView) view.findViewById(R.id.common_dialog_title);
		
		tvText.setText(getContent(json));
		((Button) vCancel).setText("退出");
		((Button) tvOk).setText("重新登录");
		vTitle.setVisibility(View.VISIBLE);
		vTitle.setText("下线通知");
		vCancel.setOnClickListener(this);
		tvOk.setOnClickListener(this);
	}
	
	private String getContent(String json) {
		System.out.println("帐号异常登录：" + json);
		long time = 0L;
		if ("1003".equals(json))
		{
			time = System.currentTimeMillis();
		}
		else
		{
			try
			{
				time = new JSONObject(json).getJSONObject("returnObject").getLong("loginTime");
			}
			catch (JSONException e)
			{
				e.printStackTrace();
				time = System.currentTimeMillis();
			}
		}
		StringBuffer sb = new StringBuffer("您的帐号于");
		sb.append(Util.getFormatTime("MM月dd日 HH:mm", time));
		sb.append("在其他终端登录");
		return sb.toString();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_dialog_cancel:
				dismiss();
				BaseSimpleActivity.clearNotification(context, DefaultConsts.DOWNLOAD_NOTIFICATION_FLAG);
				BaseSimpleActivity.exit();
				YXBaseActivity.exit();
				System.exit(0);
				break;
			case R.id.common_dialog_ok:
				dismiss();
				BaseSimpleActivity.clearNotification(context, -1);
				YXBaseActivity.exit();
				BaseSimpleActivity.exit();
				QyReaderApp.getApplication().clearActivity();
				context.startActivity(new Intent(context, NewLoginActivity.class));
				break;
		}
	}
}
